package org.thirdHero.wx.config;

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaMsgServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thirdHero.wx.service.WxSharingService;
import org.thirdHero.wx.service.impl.WxProfitSharingAbstractImpl;
import org.thirdHero.wx.service.impl.WxSharingServiceApacheHttpImpl;
import org.thirdHero.wx.service.impl.WxSharingServiceImpl;

import javax.annotation.Resource;

@Configuration
public class WxConfig {
    @Resource
    private WxProperties properties;

    @Bean
    public WxMaConfig wxMaConfig() {
        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getAppSecret());
        return config;
    }


    @Bean
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setTradeType("JSAPI");
        //payConfig.setSignType("MD5");
        payConfig.setKeyPath(properties.getKeyPath());
        return payConfig;
    }


    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
    @Bean
    public WxMaMsgService wxMaMsgService(){
        WxMaMsgService wxMaMsgService = new WxMaMsgServiceImpl(wxMaService(wxMaConfig()));
        return wxMaMsgService;
    }
    @Bean
    public WxSharingService wxSharingService(WxPayConfig payConfig){
        WxSharingService wxSharingService= new WxSharingServiceApacheHttpImpl();
        wxSharingService.setConfig(payConfig);
        return wxSharingService;
    }
}